//
//  NoticeView.swift
//  自定义弹窗提示
//
//  Created by mini on 2025/4/28.
//

import Foundation

class NoticeView: UIView {
    
    // MARK: - Properties
    private lazy var viewBG: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.9)
        GlobalMethod.setRoundView(view: view, color: .clear, numRound: 10, width: 0)
        view.addSubview(labelNotice)
        view.addSubview(loadingIcon)
        return view
    }()
    
    private lazy var labelNotice: UILabel = {
        let label = UILabel()
        GlobalMethod.setLabel(label: label, widthLimit: SCREEN_WIDTH-60, numLines: 0, isBold: false, fontNum: 14, textColor: COLOR_WHITE(), aligent: .center, text: "", bgColor: .clear)
        return label
    }()
    
    private lazy var loadingIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_loading_wait")
        imageView.contentMode = .scaleAspectFit
        imageView.widthHeight = XY(18, 18)
        imageView.isHidden = true
        return imageView
    }()
    
    private var timer: Timer?
    private var numTime: Int = 0
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(viewBG)
        isUserInteractionEnabled = false
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    // MARK: - 显示
    // 文字提示
    func showNotice(_ notice: String, time: CGFloat, frame: CGRect, viewShow: UIView) {
        timerStop()
        
        self.frame = frame
        self.loadingIcon.isHidden = true
        // 设置label
        labelNotice.fitTitle(notice, variable: SCREEN_WIDTH-60-40)
        viewBG.height = labelNotice.height + 20
        viewBG.width = labelNotice.width + 40
        
        viewBG.centerY = height/2
        viewBG.centerX = width/2.0
        labelNotice.center = CGPoint(x: viewBG.width/2.0, y: viewBG.height/2.0)
        
        // 动画
        alpha = 0.0
        transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
        viewShow.addSubview(self)
        
        UIView.animate(withDuration: 0.3) {
            self.alpha = 1.0
            self.transform = .identity
        }
        
        // 定时器
        numTime = Int(time)
        timerStart()
    }
    
    // loading 等待动画
    func showWaitLoadingNotice(_ notice: String, frame: CGRect, viewShow: UIView) {
        self.frame = frame
        self.loadingIcon.isHidden = false
        startLoadingAnimation()
        
        // 设置label
        labelNotice.fitTitle(notice, variable: SCREEN_WIDTH-60-40-loadingIcon.width-10)
        viewBG.height = labelNotice.height + 20
        viewBG.width = labelNotice.width + 40+loadingIcon.width+10
        
        viewBG.centerY = height/2
        viewBG.centerX = width/2.0
        
        loadingIcon.leftCenterY = XY(20, viewBG.height/2)
        labelNotice.leftCenterY = XY(loadingIcon.right+10, viewBG.height/2)
        
        // 动画
        alpha = 0.0
        transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
        viewShow.addSubview(self)
        
        UIView.animate(withDuration: 0.3) {
            self.alpha = 1.0
            self.transform = .identity
        }
    }
    
    // 添加动画相关方法
    private func startLoadingAnimation() {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = NSNumber(value: Double.pi * 2)
        rotationAnimation.duration = 1.0
        rotationAnimation.isCumulative = true
        rotationAnimation.repeatCount = Float.infinity
        loadingIcon.layer.add(rotationAnimation, forKey: "rotationAnimation")
    }

    func hideWaitLoadingNotice() {
        removeFromSuperview()
        loadingIcon.layer.removeAnimation(forKey: "rotationAnimation")
    }
    
    // MARK: - Private Methods
    private func timerStart() {
        if timer == nil {
            timer = Timer.scheduledTimer(timeInterval: 1,
                                      target: self,
                                      selector: #selector(timerRun),
                                      userInfo: nil,
                                      repeats: true)
        }
    }
    
    @objc private func timerRun() {
        if numTime <= 0 {
            timerStop()
            return
        }
        numTime -= 1
    }
    
    private func timerStop() {
        timer?.invalidate()
        timer = nil
        removeFromSuperview()
    }
    
    deinit {
        // 清理工作（如果需要）
    }
}
